package com.mooer.app;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.mooer.MooerApplication;
import com.mooer.R;
import com.mooer.bean.SearchLocation;
import com.mooer.bean.Shop;
import com.mooer.bean.ShopAddress;
import com.mooer.exception.SVException;
import com.mooer.http.HttpUtils;
import com.mooer.utils.ui.ProgressLinearLayout;

/**
 * @author pengfei.xu
 * @version 创建时间：2011-6-11 下午05:18:05 类说明
 */
public class BusinessDynamicList extends Activity implements OnClickListener,
		OnItemClickListener {

	private TextView title, num, update;

	private ImageView updateButton, typeButton, concernButton;

	private ListView listView;

	private BusinessAdapter adapter;

	private ProgressLinearLayout footer;

	private List<Shop> shopList = new ArrayList<Shop>();

	private boolean isCoupon;

	private MooerApplication mooerApp;
	
	private SearchLocation search;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.business_dynamic_list);
		mooerApp = (MooerApplication) getApplication();
		Intent intent = getIntent();
		title = (TextView) findViewById(R.id.title);
		update = (TextView) findViewById(R.id.update);
		updateButton = (ImageView) findViewById(R.id.update_btn);
		typeButton = (ImageView) findViewById(R.id.type_btn);
		concernButton = (ImageView) findViewById(R.id.concern_btn);
		listView = (ListView) findViewById(R.id.listview);
		footer = (ProgressLinearLayout) LayoutInflater.from(this).inflate(
				R.layout.progress_small, null);
		footer.initView();
		listView.addFooterView(footer);
		adapter = new BusinessAdapter(this, shopList);
		listView.setAdapter(adapter);

		if ("coupon".equals(intent.getStringExtra("type"))) {
			update.setHint("全部地区");
			isCoupon = true;
		}

		updateButton.setOnClickListener(this);
		typeButton.setOnClickListener(this);
		concernButton.setOnClickListener(this);
		listView.setOnItemClickListener(this);
		search = new SearchLocation();
		String layoutType = getIntent().getStringExtra("type");
		if("coupon".equals(layoutType)){
			new BusinessDynamicListTask(0).execute(search);
		}else{
			new BusinessDynamicListTask(1).execute(search);
		}
		
	}

	@Override
	public void onClick(View v) {
		if (v == updateButton) {

		}

	}

	public class BusinessDynamicListTask extends
			AsyncTask<SearchLocation, Integer, List<Shop>> {
		private boolean isException;
		private int type;
		
		public BusinessDynamicListTask(int type){
			this.type = type;
		}
		
		@Override
		protected List<Shop> doInBackground(SearchLocation... params) {
			List<Shop> list = null;
			try {
				if (type == 0) {
					list = HttpUtils.fetchShopUpdateInfo(-1, params[0], 0, 15);
				} else if (type == 1) {
					list = HttpUtils
							.fetchLatestShopList(params[0], 0, 15);
				}
				for (Shop shop : list) {
					shop.setIcon(mooerApp.getShopListDefaultIcon());
				}
			} catch (Exception e) {
				isException = true;
				e.printStackTrace();
			}
			return list;
		}

		@Override
		protected void onPostExecute(List<Shop> result) {
			super.onPostExecute(result);
			if (isException) {
				footer.setVisibility(View.GONE, View.GONE, View.VISIBLE,
						View.VISIBLE);
			} else {
				if (result.size() > 0) {
					listView.removeFooterView(footer);
					shopList.addAll(result);
					adapter.notifyDataSetChanged();
					/*num.setText(getString(R.string.business_num,
							shopList.size()));*/
					//new BitmapTask().execute("");
				} else {
					footer.setVisibility(View.GONE, View.GONE, View.VISIBLE,
							View.GONE);
					footer.setText(null, "暂无商家列表");
				}
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		Shop shop = shopList.get(position).copy();
		Intent intent = new Intent(BusinessDynamicList.this, Business.class);
		intent.putExtra("businessInfo", shop);
		startActivity(intent);

	}

}
